

jQuery(document).ready(function() {
	
	initTable();
	
	App.setPage("role");  //Set current page
	App.init(); //Initialise plugins and elements
	
	bindDict("roletype", "DM_ROLETYPE", null);

	//列表查询事件
	$('#search').click(function(){
		$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
	});
	
	
	//新增字典
	$('#addRole').click(function(){
    	setModalSize();
    	
    	$('#ifModal').attr("src", "add.html");
    	
		$('#modalTile').html("角色新增");
		$("#modal").modal('show');
	});
	
	//批量删除
	$('#batchDelete').click(function(){
		
		var data = $('#table').bootstrapTable('getSelections');
		if(data.length <= 0){
			window.wxc.xcConfirm('请勾选要<strong>批量删除</strong>的角色数据', window.wxc.xcConfirm.typeEnum.info);
			return;
		}
		var rolecodes = "";
		for(var i = 0; i < data.length; i++){
			if(data[i].roletype != '02')
				rolecodes += data[i].rolecode + ",";
		}
		
		rolecodes = rolecodes.substring(0, rolecodes.length - 1);
		window.wxc.xcConfirm('是否<strong>批量删除</strong>角色数据?<br /><span style="color:red;">['+ rolecodes +']</span>', window.wxc.xcConfirm.typeEnum.confirm,{
			onOk:function(v){
				$.ajax({ 
					url: basePath() + 'role/batchDelete.do',
					data: {"rolecodes": rolecodes},
					dataType: "json",
					success: function(data){	            						
						if(data.result == 1){        							        							
	        	            window.wxc.xcConfirm('删除角色['+ data.datas[0].rolecode +']成功!', window.wxc.xcConfirm.typeEnum.info);
				        	$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
						}
			      	}
				});
			}
		});
	});
	
	//保存数据事件
	$('#save').click(function(){
		$("#ifModal")[0].contentWindow.save();
	});
	
	
	//清空表单数据事件
	$("#modalDict").on("hidden.bs.modal", function() {
		$('#tableDictDetail').find('tbody').find('tr').not('.no-records-found').remove();	
		if($('#' + tableID).find('tbody').children('tr').length == 0){
			var trStr = '<tr class="no-records-found">	'+
		    '   <td colspan="7" style="font-size:14px;">			'+
			'		单充底部“新增行”增加字典！		'+
			'	</td>						'+
			'</tr>';
			
			$('#' + tableID).find('tbody').append(trStr);
		}
		
		$(this).find('[type="text"]').val('');
		$(this).find('[type="checkbox"]').prop("checked",true);         	
	});
});		



function initTable(){		
	$('#table').bootstrapTable({
		method: 'post',
	    url: basePath() + 'role/list.do',
	    pagination: true, //分页
	    dataType: "json",
        contentType: "application/json",
        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        striped: true,      //是否显示行间隔色
        sidePagination: "server",
        ////查询参数,每次调用是会带上这个参数，可自定义                         
        queryParams:function(params) {
            return {
                	pageNum: params.offset / params.limit + 1,
                	pageSize: params.limit,
                	rolecode: $('#rolecode').val(),
                	rolename: $('#rolename').val(),
                	roletype: $('#roletype').val()
                };
        },
	    columns: [{
            field : 'checked',
            checkbox : true

        }, {  
            field : 'number',  
            title : '序号',  
            align : 'center',
            width : '80px',
            formatter : function(value, row, index) {  
// 		                return index + 1;  
                var page = $('#table').bootstrapTable("getPage");  
                return page.pageSize * (page.pageNumber - 1) + index + 1;  
            }  
        }, {
	        field: 'rolecode',
	        title: '角色代码',
	        formatter:function(value,row,index){
	        	return  '<a onclick="showDetail(\''+ value +'\')">' + value + '</a>';
	        }
	    }, {
	        field: 'rolename',
	        title: '角色名称'
	    },  {
	        field: 'roletype',
	        title: '角色类型',
	        formatter:function(value,row,index){
	        	return  value == "01" ? "系统角色" : "4A角色";
	        }
	    },  {
            field: 'operate',
            title: '操作',
            align: 'center',
            width: '300px',
            events: operateEvents,
            formatter: operateFormatter
        } ]
	});
	
	//表格加载完事件，去除角色类型是“4A角色”的复选框
	$("#table").on('load-success.bs.table', function (e, data) {
        $(this).find('[type="checkbox"]').each(function(){
        	var tdhtml = $(this).parent().next().next().next().next().html();
        	if(tdhtml == '4A角色'){
        		$(this).remove();
        	}
        })
    })
}

function operateFormatter(value, row, index) {
	if(row.roletype != '02'){
	    return [
	    	'<button type="button" class="Right btn btn-default  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-list"></span>菜单授权</button>',
	        '<button type="button" class="Edit btn btn-warning  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-edit"></span>编辑</button>',
	        '<button type="button" class="Delete btn btn-danger  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-minus"></span>删除</button>'
	    ].join('');
	}
}

window.operateEvents = {
	'click .Right':function(e,value,row,index){
		var rolecode = row.rolecode;
    	
    	setModalSize();
    	
    	$('#ifModal').attr("src", "menuright.html?rolecode=" + rolecode);
    	var title = '(<span style="font-size:15px;font-weight:550">'+rolecode+'-'+row.rolename+')</span>'
    	
		$('#modalTile').html('菜单授权'+title);
		$("#modal").modal('show');
	},
	'click .Edit': function (e, value, row, index) {
    	var rolecode = row.rolecode;
    	
    	setModalSize();
    	
    	$('#ifModal').attr("src", "edit.html?rolecode=" + rolecode);
    	
		$('#modalTile').html("角色修改");
		$("#modal").modal('show');
    },
    'click .Delete': function (e, value, row, index) {

		var rolecode = row.rolecode;
		window.wxc.xcConfirm('是否<strong>删除</strong>角色数据?<br /><span style="color:red;">['+ rolecode +']</span>', window.wxc.xcConfirm.typeEnum.confirm,{
			onOk:function(v){
				$.ajax({ 
					url: basePath() + 'dict/delete.do',
					data: {"dictcode": dictcode},
					dataType: "json",
					success: function(data){	            						
						$.ajax({ 
        					url: basePath() + 'role/delete.do',
        					data: {"rolecode": rolecode},
        					dataType: "json",
        					success: function(data){	            						
        						if(data.result == 1){
        				        	$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
        						}
        			      	}
        				});
			      	}
				});
			}
		});
    }
 };


function showDetail(rolecode){
	setModalSize();
	
	$('#ifModal').attr("src", "detail.html?rolecode=" + rolecode);
	
	$('#modalTile').html("角色详细");
	$("#modal").modal('show');
}

